package org.saxion.aktiehuis.view;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;

import org.saxion.aktiehuis.model.BesteldProduct;
import org.saxion.aktiehuis.model.Product;
import org.saxion.aktiehuis.model.ProductSoort;
import org.saxion.aktiehuis.services.BestellingManagement;
import org.saxion.aktiehuis.services.ProductManagement;

import java.awt.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;



public class ProductenView extends JPanel {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public ProductManagement productManagement;
    public BestellingManagement bestellingManagement;
    
    public ProductenTabelModel model;
    
    private List<Product> producten = null;
    private List<ProductSoort> productSoorten = null;
    private ProductSoort productSoort = null;
	
    private JTable table;
    private JButton bestelBtn;
    private JComboBox soortenBox;
    
    private int bestelId;
	private int klantId;
   
	/**
	 * Create the panel.
	 * @throws Exception 
	 */
	public ProductenView() throws Exception {

		initialize();

	} 
	
	public void setBestelId(int id)
	{
		bestelId = id;
	}

	
	private void initialize()
	{
		setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		bestelBtn = new JButton("Bestel");
		bestelBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bevestigBestelling();
			}
		});

		soortenBox = new JComboBox();
		
		
		soortenBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				changeFilter();
			}
		});
		
		table = new JTable();
		
		add(new JScrollPane(table));
		add(bestelBtn);
		add(soortenBox);

	}
	
	public void showProducts()
	{	
		
		try {
			if(productSoort == null){
				producten = productManagement.getProducten();
			}
			else{
				producten = productManagement.getProducten(productSoort);
			}
				
		} catch (Exception e) {
			System.out.println("Error getting products");
			e.printStackTrace();
		}

		model = new ProductenTabelModel(producten);
	    model.setEditableCol(3); //aantal
	    table.setModel(model);
	    table.getColumnModel().getColumn( 3 ).setCellEditor(new SpinnerEditor());
	    
	}
	
	public void getProductSoorten()
	{
		 	try {
				productSoorten = productManagement.getProductenSoorten();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		 	soortenBox.removeAllItems();
			soortenBox.addItem("Alles");
			for(ProductSoort productSoort : productSoorten)
			{
				soortenBox.addItem(productSoort);
			}
	}
	
	private void bevestigBestelling()
	{

		HashMap<Product ,Integer> producten = model.getProducts();
		
		List<BesteldProduct> bestelling = new ArrayList<BesteldProduct>();

		for (Entry<Product, Integer> product : producten.entrySet())
		{
			if(product.getValue() > 0){
				BesteldProduct besteldProduct = new BesteldProduct();
				besteldProduct.setProduct((Product) product.getKey());
				besteldProduct.setAantal(product.getValue());
	
			    bestelling.add(besteldProduct);
			    
			 	}
		}
		
		try {
			
			bestellingManagement.addBestelling(bestelId, bestelling);
			
		} catch (Exception e) {
		
			e.printStackTrace();
		}
		
		System.out.println("Bestelling is geplaatst!");
	}
	
	public void changeFilter()
	{
		if(soortenBox.getSelectedItem() != null && soortenBox.getSelectedItem() != "Alles")
			this.productSoort = (ProductSoort) soortenBox.getSelectedItem();
		else
			this.productSoort = null;
		
		showProducts();
	}
	
	
	public void setProductManagement(ProductManagement productManagement) throws Exception
	{
		this.productManagement =productManagement;
	}
	
	public void setBestellingManagement(BestellingManagement bestellingManagement) throws Exception
	{
		this.bestellingManagement =bestellingManagement;
	}
	
	public class SpinnerEditor extends DefaultCellEditor
	{
	    /**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private JSpinner spinner;

	    public SpinnerEditor()
	    {
	        super( new JTextField() );
	        spinner = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
	        spinner.setBorder( null );
	    }

	    public Component getTableCellEditorComponent(
	        JTable table, Object value, boolean isSelected, int row, int column)
	    {
	        spinner.setValue( value );
	        return spinner;
	    }

	    public Object getCellEditorValue()
	    {
	        return spinner.getValue();
	    }
	}

	public void setKlantId(int klantId) {
		this.klantId = klantId;
	}

}
